/**
 * @file gjson.h
 * @brief json analyzer based on glib
 * @author JakeYang <add358@gmail.com>
 * @version 0.1
 * @date 2016-02-24
 */

#include "gjson.h"
#include <stdio.h>

GJson *g_json_parse_file(const gchar *filename)
{
    g_return_val_if_fail(("gjson parse file", filename != NULL), NULL);

    GMappedFile *mappedFile = NULL;
    GError *err = NULL;
    GJson *json = NULL;

    mappedFile = g_mapped_file_new(filename, FALSE, &err);
    if (err) {
        g_error("Unable to read file: %s", err->message);
        g_error_free(err);
        return NULL;
    }

    json = g_json_parse_data(g_mapped_file_get_contents(mappedFile));

    g_mapped_file_unref(mappedFile);

    return json;
}

inline GJson *g_json_parse_data(gchar *data)
{
    g_return_val_if_fail(("gjson parse data", data != NULL), NULL);

    GJsonData *d = g_json_data_new(data);

    GJson *json = g_json_object_parse(d);

    g_json_data_free(d, FALSE);

    return json;
}

inline void g_json_destroy(GJson *json)
{
    g_json_object_free(json);
}

inline gchar *g_json_print(GJson *json)
{
    GJsonText *t = g_malloc0(sizeof(GJsonText));
    t->str = g_string_new_len(NULL, 512);

    GString *msg = NULL;
    g_json_object_print(json, t);
    g_string_append(t->str, "\n");
    msg = t->str;
    g_free(t);
    return g_string_free(msg, FALSE);
}


inline GJson *g_json_get_object(GJson *json, const gchar *str)
{
    g_return_val_if_fail(("gjson get object", NULL != json && NULL != str), NULL);
    GJsonValue *v = g_json_object_get_value(json, str, GJSON_OBJECT, NULL);
    return (v ? v->value : NULL);
}

inline GJsonArray *g_json_get_array(GJson *json, const gchar *str)
{
    g_return_val_if_fail(("gjson get array", NULL != json && NULL != str), NULL);
    GJsonValue *v = g_json_object_get_value(json, str, GJSON_ARRAY, NULL);
    return (v ? v->value : NULL);
}

inline gchar *g_json_get_string(GJson *json, const gchar *str)
{
    g_return_val_if_fail(("gjson get string", NULL != json && NULL != str), NULL);
    GJsonValue *v = g_json_object_get_value(json, str, GJSON_STRING, NULL);
    return (v ? v->value : NULL);
}

gint g_json_get_int(GJson *json, const gchar *str, GError **err)
{
    g_return_val_if_fail(("gjson get int", NULL != json && NULL != str), 0);

    GJsonValue *v = g_json_object_get_value(json, str, GJSON_NUMBER, err);
    gint r = 0;
    if (NULL == v) {
        return 0;
    }

    sscanf(v->value, "%d", &r);
    return r;
}

gdouble g_json_get_double(GJson *json, const gchar *str, GError **err)
{
    g_return_val_if_fail(("gjson get double", NULL != json && NULL != str), 0);

    GJsonValue *v = g_json_object_get_value(json, str, GJSON_NUMBER, err);
    gdouble r = 0;

    if (NULL == v ) {
        return 0;
    }

    sscanf(v->value, "%lf", &r);

    return r;
}

inline gboolean g_json_get_gboolean(GJson *json, const gchar *str, GError **err)
{
    g_return_val_if_fail(("gjson get gboolean", NULL != json && NULL != str), FALSE);
    GJsonValue *v = g_json_object_get_value(json, str, GJSON_BOOLEAN, err);
    return (v ? GPOINTER_TO_INT(v->value) : FALSE);
}








